import { defineStore } from 'pinia'

export const useMineStore = defineStore('mine', {
    state: () => {
        return {
            userInfo: '',
            token: '',
            wx_code: ''
        }
    },
    actions: {
        // 保存用户信息
        saveUserInfo (val) {
            let arr = [
                'userInfo',
                'token',
                'wx_code'
            ]
            arr.forEach(item => {
                if (item in val) {
                    this[item] = val[item]
                }
            })
        },
        // 清空用户信息
        clearUserInfo () {
            const array = Object.keys(this.$state)
            array.forEach(key => this.$state[key] = '')
            if (
                Object.keys(this.userInfo).length == 0
                &&
                !this.token
            ) {
                return Promise.resolve(true)
            } else {
                return Promise.resolve(false)
            }
        }
    },
    persist: { enabled: true }
})
